<template>
  <div class="login">
    <span class="login-title">加入</span>
    <span class="login-title">Can I Speak</span>
    <div class="login-form">
      <span></span>
      <div class="login-form-icon">
        <p>用户名</p>
        <div>
          <span class="iconfont icon-user"></span>
          <input type="text" name="username" v-model="username" />
        </div>
      </div>
      <div class="login-form-icon">
        <p>密码</p>
        <div>
          <span class="iconfont icon-password"></span>
          <input type="password" name="password" v-model="password" />
        </div>
      </div>
      <div class="login-form-icon" v-if="!isLogin">
        <p> 昵称</p>
        <div>
          <span class="iconfont icon-name"></span>
          <input type="text" name="nickname" v-model="nickname" />
        </div>
      </div>
      <el-checkbox v-if="isLogin" v-model="isRemember">记住密码</el-checkbox>
      <div class="login-botton">
        <span class="login-botton-login" @click="hanldeLogin">登录</span>
      </div>
      <div class="login-botton-register">
        <span>没有账号?</span>
        <span @click="hanldeRegister">点击注册</span>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref } from "vue";
import { accountRegister, accountLogin } from "@/api/login/index"
import { ElMessage } from 'element-plus'
import router from "@/router";
import { userStore } from "@/store/user";
import { getUserInfo } from '@/api/user'

import "element-plus/theme-chalk/el-message.css"
const username = ref("");
const password = ref("");
const nickname = ref("");
let isLogin = ref(true)
const isRemember = ref(false)
// storeToRefs

const user_store = userStore()
// 注册
function hanldeRegister() {
  if (isLogin.value) {
    isLogin.value = false
    return
  }
  if (!username.value || !password.value || !nickname.value) {
    return ElMessage('请正确输入账号密码或用户名')
  }
  accountRegister({ username: username.value, password: password.value, nickname: nickname.value }).then(res => {
    ElMessage(res.data.message)
    if (res.status === 200) {
      accountLogin({ username: username.value, password: password.value }).then((res) => {
        user_store.changeLoginState(res.data.token || '')
        router.push("/userInfo")
      })
    }
  })
};
//登录
function hanldeLogin() {
  if (!isLogin.value) {
    isLogin.value = true
    return
  }
  if (!username.value || !password.value) {
    return ElMessage('请正确输入账号密码')
  }
  accountLogin({ username: username.value, password: password.value }).then(res => {
    user_store.changeLoginState(res.data.token || '')
    localStorage.setItem('username', username.value)
    localStorage.setItem('password', password.value)
    ElMessage(res.data.message)
    router.replace("/main")
    // 获取用户信息
    getUserInfo().then(res => {
      user_store.changeUserInfoState(res.data.data)
    })

  })
}
</script>

<style lang="less" scoped>
.login {
  position: absolute;
  height: 100%;
  width: 100%;
  font-size: 18px;
  background-image: url("@/assets/img/bg.png");
  background-repeat: no-repeat;
  background-size: cover;

  &-title {
    margin: 40px 60px;
    font-weight: 900;
    font-size: 60px;
    display: flex;
  }

  // color: @mainColor;
  &-form {
    position: absolute;
    bottom: 0;
    height: 60%;
    width: 100%;
    display: flex;
    flex-direction: column;
    padding: 30px 0;
    // -ms-flex-align: start;
    align-items: center;
    justify-content: space-around;
    background-color: #ffffff;
    border-top-left-radius: 40px;
    border-top-right-radius: 40px;
    color: #767677;

    &-icon {
      width: 600px;

      >p {
        font-weight: 400;
        font-size: 40px;
      }

      >div {
        >span {
          position: absolute;
          font-size: 40px;
          font-weight: 800;
          color: #0fd992;
        }

        >input {
          font-size: 40px;
          width: 600px;
          background: none;
          outline: none;
          border: none;
          padding-left: 50px;
          border-bottom: 1px solid #dfe0e1;
        }

        input:focus {
          border: none;
          border-bottom: 1px solid #dfe0e1;
        }
      }
    }

    >span {
      width: 120px;
      height: 15px;
      border-radius: 50px;
      background-color: #e0e1e3;
    }
  }

  &-botton {
    margin-top: 10px;

    &-login {
      display: block;
      padding: 35px;
      width: 600px;
      background-color: #07d88e;
      text-align: center;
      border-radius: 50px;
    }

    &-register {
      >span {
        &:last-child {
          margin-left: 10px;
          color: #07d88e;
        }
      }
    }
  }
}

.el-input {
  height: 50px;
  border-radius: 12px;
  overflow: hidden;
}
</style>